package zhuyun.code.horizontalscrollview

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.Scroller
import kotlin.math.abs

/**
 * @author: Administrator
 * @date: 2022/1/1
 */
class HorizontalScrollView(private val mContext:Context, private val attributeSet: AttributeSet?, private val defStyleAttr:Int):ViewGroup(mContext,attributeSet,defStyleAttr) {
    constructor(mContext: Context,attributeSet: AttributeSet?):this(mContext,attributeSet,0)
    constructor(mContext: Context):this(mContext,null)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        measureChildren(widthMeasureSpec, heightMeasureSpec)
    }
    
    private var leftBorder = 0
    private var rightBorder = 0 

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        if(changed){
            val childCount = childCount
            for(i in 0 until childCount){
                val child = getChildAt(i)
                child.layout(i*child.measuredWidth,0,(i+1)*child.measuredWidth,child.measuredHeight)
            }
            leftBorder = getChildAt(0).left;
            rightBorder = getChildAt(childCount - 1).right
        }
        
    }

    private var currentX = 0
    private var currentY = 0
    private var lastX = 0
    private var lastY = 0
    private val mScroller = Scroller(mContext);
      //外部拦截法
//    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
//        currentX = ev!!.x.toInt()
//        currentY = ev!!.y.toInt()
//        when(ev!!.action){
//            MotionEvent.ACTION_DOWN->{
//
//            }
//            MotionEvent.ACTION_MOVE->{
//                val dx = currentX - lastX
//                val dy = currentY - lastY
//                if(abs(dx) > abs(dy)){
//                    return true
//                } else {
//
//                }
//            }
//            MotionEvent.ACTION_UP->{
//
//            }
//        }
//        lastX = currentX
//        lastY = currentY
//        return super.onInterceptTouchEvent(ev)
//    }
    //内部拦截法
    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
          when(ev!!.action){
            MotionEvent.ACTION_DOWN->{
                return false
            }
            else->{
                return true
            }

        }

        return super.onInterceptTouchEvent(ev)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return super.dispatchTouchEvent(ev)
    }

    private var isFirstActionMove = true;

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        currentX = event!!.rawX.toInt()
        currentY = event!!.rawY.toInt()
        when(event!!.action){
            MotionEvent.ACTION_DOWN->{

            }
            MotionEvent.ACTION_MOVE->{
                if(!isFirstActionMove){
                    val distance = currentX - lastX
                    if(distance > 0) {//向右滑
                        if(scrollX + (-distance) < leftBorder){
                            scrollTo(leftBorder,0)
                            return true
                        }
                    } else {//向左滑动
                        if(scrollX + (-distance) + width > rightBorder){
                            scrollTo(rightBorder - width, 0)
                            return true
                        }
                    }
                    scrollBy(-distance,0)
                } else {
                    isFirstActionMove = false
                }


            }
            else->{
                val page = (scrollX + width/2)/width
                mScroller.startScroll(scrollX,scrollY,page * width - scrollX, 0)
                invalidate()
                isFirstActionMove = true
            }

        }
        lastX = currentX
        lastY = currentY
        return super.onTouchEvent(event)
    }

    override fun computeScroll() {
        if(mScroller.computeScrollOffset()){
            scrollTo(mScroller.currX,mScroller.currY)
            invalidate()
        }
    }

}